/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem.actions;

import java.io.File;

import org.kommando.core.action.AbstractAction;
import org.kommando.core.action.ActionExecutionException;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.core.catalog.TextObject;
import org.kommando.filesystem.core.FileObject;
import org.kommando.filesystem.core.FileSystemObject;
import org.kommando.filesystem.core.FolderObject;

/**
 * @author Peter De Bruycker
 */
public class RenameAction extends AbstractAction {

    private static final String ID = "file:rename";

    @Override
    public CatalogObject execute(CatalogObject direct, CatalogObject indirect) throws ActionExecutionException {
        File file = direct.getExtension(FileSystemObject.class).getFile();
        String name = indirect.getExtension(String.class);

        file.renameTo(new File(file.getParent(), name));

        return null;
    }

    @Override
    public Class<?>[] getIndirectTypes() {
        return new Class[] { String.class };
    }

    @Override
    public Class<?>[] getDirectExtensionTypes() {
        return new Class<?>[] { FileObject.class, FolderObject.class };
    }

    @Override
    public boolean isIndirectMandatory(CatalogObject direct) {
        return true;
    }

    @Override
    public CatalogObject getDefaultIndirectObjectValue(CatalogObject directObject) {
        return new TextObject(directObject.getName());
    }

    @Override
    public String getId() {
        return ID;
    }

    @Override
    public String getDescription() {
        return "Rename the file";
    }

    @Override
    public String getName() {
        return "Rename...";
    }

}
